package com.hornsun.manage.common.contentType;

import com.hornsun.domain.constant.types.InfoType;
import com.hornsun.domain.dbo.ContentType;
import com.hornsun.repository.CommodityRepository;
import com.hornsun.repository.ContentTypeRepository;
import com.hornsun.repository.InformationRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/5/9.
 */
@RestController
@RequestMapping("/manage/contentType")
public class ContentTypeApi {

    @Resource
    private ContentTypeRepository contentTypeRepository;

    @Resource
    private InformationRepository informationRepository;

    @Resource
    private CommodityRepository commodityRepository;

    /**
     * 通过type获取对应类型
     * @param type
     * @return
     */
    @GetMapping("/getByType")
    public ResponseEntity<List<ContentType>> getContentType(@RequestParam("type")int type){
        return new ResponseEntity<List<ContentType>>(contentTypeRepository.queryContentType(type), HttpStatus.OK);
    }

    /**
     * 重置资讯类型
     * @param contentTypeList
     * @return
     */
    @Transactional
    @PostMapping({"/setInformationType"})
    public ResponseEntity<?> setInformationType(@RequestBody ContentType[] contentTypeList)
    {
        setContentType(contentTypeList, InfoType.INFORMATION.getType());
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 重置商品类型
     * @param contentTypeList
     * @return
     */
    @Transactional
    @PostMapping({"/setCommodityType"})
    public ResponseEntity<?> setCommodityType(@RequestBody ContentType[] contentTypeList)
    {
        setContentType(contentTypeList, InfoType.COMMODITY.getType());
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 修改类名
     * @param contentType
     * @return
     */
    @Transactional
    @PostMapping("/updateTypeName")
    public ResponseEntity<?> updateTypeName(ContentType contentType){
        contentTypeRepository.update(contentType);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    @GetMapping("/checkDelete")
    public ResponseEntity<?> checkDelete(@RequestParam("guid")String typeId, @RequestParam("type")int type){
        if (type==InfoType.INFORMATION.getType()){
            return new ResponseEntity<Object>(informationRepository.hasContentByTypeId(typeId),HttpStatus.OK);
        }else if(type==InfoType.COMMODITY.getType()){
            return new ResponseEntity<Object>(commodityRepository.hasContentByTypeId(typeId),HttpStatus.OK);
        }else {
            throw new IllegalArgumentException("type is not exist");
        }
    }




    private void setContentType(ContentType[] contentTypeList, Integer type) {
        this.contentTypeRepository.deleteByType(type);
        if (contentTypeList.length > 0) {
            for (int i = 0; i < contentTypeList.length; i++)
            {
                ContentType contentType = new ContentType();
                if (contentTypeList[i].getId()==null||contentTypeList[i].getId().isEmpty()){
                    contentType.setId(UUID.randomUUID().toString());
                }else {
                    contentType.setId(contentTypeList[i].getId());
                }
                contentType.setName(contentTypeList[i].getName());
                contentType.setType(contentTypeList[i].getType());
                contentType.setTypeIndex(i);
                this.contentTypeRepository.insert(contentType);
            }
        }
    }




}
